//
//  SettingsViewController.m
//  WeightMaster
//
//  Created by sungeo on 15/8/21.
//  Copyright (c) 2015年 buddysoft. All rights reserved.
//

#import "SettingsViewController.h"
#import "BDFoundation.h"
#import "WMAppSetting.h"
#import "WMModels.h"
#import "EditGenderViewController.h"
#import "EditHeightViewController.h"
#import "EditWeightViewController.h"
#import "WMSegmentedView.h"
#import <NSObject+GLPubSub.h>
#import <EXTScope.h>
#import <Masonry.h>

typedef enum{
    SettingsCellRowHeight = 0,
    SettingsCellRowGender,
    SettingsCellRowGoal,
}SettingsCellRowTag;

@interface SettingsViewController (){
    NSArray * _sections;
    NSArray * _sectionHeaders;
    
    WMSegmentedView * _segmentedView;
}

@end

@implementation SettingsViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    self.title = @"设置";
    
    _sectionHeaders = @[@"个人资料", @"体重单位"];
    _sections = @[@[@"身高", @"性别", @"目标"], @[@"单位"]];
    
    @weakify(self);
    [self subscribe:WMSettingsChanged handler:^(GLEvent * event){
        @strongify(self);
        
        dispatch_async(dispatch_get_main_queue(), ^{
           [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
        });
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)dealloc{
    [self unsubscribeAll];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_sections[section] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return _sectionHeaders[section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 60;
    }else{
        return 74;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * sIdentifier = @"SettingsCell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:sIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:sIdentifier];
    }
    
    if (indexPath.section == 0) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text = _sections[indexPath.section][indexPath.row];
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        WMSegmentedView * view = [[WMSegmentedView alloc] init];
        view.delegate = self;
        [cell.contentView addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker * maker){
            const CGFloat Margin = 17;
            maker.leading.equalTo(cell.contentView.mas_left).offset(Margin);
            maker.trailing.equalTo(cell.contentView.mas_right).offset(-Margin);
            maker.top.equalTo(cell.contentView.mas_top).offset(Margin/2);
            maker.bottom.equalTo(cell.contentView.mas_bottom).offset(-Margin/2);
        }];
        _segmentedView = view;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        WMAppSetting * appSetting = [WMAppSetting sharedInstance];
        SettingsCellRowTag rowTag = (SettingsCellRowTag)indexPath.row;
        switch (rowTag) {
            case SettingsCellRowHeight:
                cell.detailTextLabel.text = [WMHelper stringValueForHeight:appSetting.height.floatValue];
                break;
            case SettingsCellRowGender:
                cell.detailTextLabel.text = [WMHelper stringValueForGender:(WMGender)(appSetting.gender.integerValue)];
                break;
            case SettingsCellRowGoal:{
                if ([appSetting isSetGoalWeight]) {
                    NSString * weightString = [WMHelper stringValueForWeight:appSetting.goalWeight withUnit:appSetting.weightUnit];
                    NSString * unitString = [WMHelper stringValueForUnit:appSetting.weightUnit];
                    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@%@", weightString, unitString];
                }else{
                    cell.detailTextLabel.text = @"请设置";
                }
                break;
            }
            default:
                break;
        }
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            WMWeightUnit unit = (WMWeightUnit) [WMAppSetting sharedInstance].weightUnit.integerValue;
            if (unit == WMWeightUnitKiloGram) {
                _segmentedView.selectedIndex = 0;
            }else{
                _segmentedView.selectedIndex = 1;
            }
        }
    }
}

#pragma mark - WMSegmentedView delegate
- (void)wmSegmentedView:(WMSegmentedView *)view changedWithNewIndex:(NSNumber *)index{
    NSInteger newIndex = [index integerValue];
    switch (newIndex) {
        case 0:
            NSLog(@"公斤");
            [WMAppSetting sharedInstance].weightUnit = @(WMWeightUnitKiloGram);
            break;
        case 1:
            NSLog(@"斤");
            [WMAppSetting sharedInstance].weightUnit = @(WMWeightUnitJin);
            break;
        default:
            break;
    }
    
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[WMAppSetting sharedInstance] syncData];
//    });
}


#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case SettingsCellRowHeight:
            {
                EditHeightViewController * vc = STORYBOARDVC(@"Main", @"EditHeightViewController");
                vc.editMode = YES;
                [self.navigationController pushViewController:vc animated:YES];
                break;
            }
            case SettingsCellRowGender:
            {
                EditGenderViewController * vc = STORYBOARDVC(@"Main", @"EditGenderViewController");
                vc.editMode = YES;
                [self.navigationController pushViewController:vc animated:YES];
                break;
            }
            case SettingsCellRowGoal:
            {
                EditWeightViewController * vc = STORYBOARDVC(@"Main", @"EditWeightViewController");
                vc.editMode = YES;
                [self.navigationController pushViewController:vc animated:YES];
                break;
            }
        }
    }
}

@end
